<?php
class GuestsController extends AppController {
	var $name = 'Guests';
	var $components = array('Uploader.Uploader');
	var $errors = array();
	//var $uses=array('Image');

	function beforeFilter() {
		parent::beforeFilter();
		$this->Uploader->uploadDir = 'files/uploads/guest/'; //thu muc chua file upload
        $this->Uploader->enableUpload = true;
        $this->Uploader->maxFileSize = '2M'; // quy dinh dung luong duoc upload len toi da la 2 Megabytes
        $this->Uploader->maxNameLength = 25;//do dai cua ten file
        $this->Uploader->tempDir = TMP;
		$this->Auth->allow('pages');
	}	
	
	function index($fid=NULL) {
		$this->Guest->recursive = 1;
		$conds = array('Guest.status' =>2);
 		
		$this->paginate = array(
			'conditions'=>$conds,
			'order'=>'Guest.id DESC'
		);
		$this->set('guests', $this->paginate());
		//debug($this->paginate());
	}
	
	function chitiet($id = null) {
  		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại bài viết này.', true));
			$this->redirect(array('action' => 'index'));
		}
		$conditions = array('Guest.status'=>2);
		$guest = $this->Guest->read(null, $id); 		
 		$this->set('guest',$guest);
		$otherguest = $this->Guest->find('all',array('conditions'=>array('Guest.id <>'=>$id,'Guest.status'=>2),'limit'=>10,'order'=>'Guest.id DESC'));
		$this->set(compact('otherguest'));
 	}
	
   	function admin_index() {		
		$this->layout='admin';
		$this->Guest->recursive = 0;
		$conditions = '';		
		$key = $this->data['Guest']['key'];
 		if($key!= null){
			$conditions = array('Guest.name LIKE '=>'%'.$key.'%');
		}		
		$this->paginate = array(
			'conditions'=>$conditions,
			'order'=> 'Guest.id DESC'
		);
		$this->set('guests', $this->paginate());
		//debug($this->paginate);		
		$this->set(compact('key'));
	}


	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại sản phẩm này.', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('guest', $this->Guest->read(null, $id));
	}

	function admin_add() {
		//debug($this->data);
		if (!empty($this->data)) {
			$filename = $this->Uploader->upload('image');
			$this->Guest->create();
			$users_id = $this->Auth->user('id');
			$this->data['Guest']['image']  = $filename['name'];
			if ($this->Guest->save($this->data)) {
				$this->Session->setFlash(__('Đã lưu.', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không được lưu. Vui lòng thử lại.', true));
			}
		}		
	}

	 function admin_edit($id = null) {
		$files = $this->Guest->find('list',array(
			'conditions'=>array('Guest.id'=>$id),
			'fields'=>'image')
		);
		$file ='';
		$file = "files" . DS . "uploads/guest" . DS .$files[$id];
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Không tồn tại khách hàng này.', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->data['Guest']['image'] = $files[$id];
            if ($data = $this->Uploader->upload('image')) {
				$tmpname  = $data['name'];
				if(is_file($file)){
					unlink($file);
				}
				$this->data['Guest']['image'] = $tmpname;
            }
			if ($this->Guest->save($this->data)) {
				$this->Session->setFlash(__('Đã lưu.', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thông tin không được lưu. Vui lòng thử lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Guest->read(null, $id);
		}	
		
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại khách hàng này !', true));
			$this->redirect(array('action'=>'index'));
		}
		if($id){
			$files = $this->Guest->find('first',array(
			'conditions'=>array('Guest.id'=>$id),
			'fields'=>'image')
		);
		$file = "files/uploads/guest/".$files['Guest']['image'];
			if(is_file($file)){
				unlink($file);
			}
		}
		if ($this->Guest->delete($id)) {
			$this->Session->setFlash(__('Đã xóa !', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Có lỗi xảy ra. Chưa xóa được khách hàng !', true));
		$this->redirect(array('action' => 'index'));
	}
	function admin_mutildelete($str = null){
        if($str){
            $arrid=explode(',',$str);
        }else{
    		$this->Session->setFlash(__('Có lỗi xảy ra. Chưa xóa được khách hàng !', true));
    		$this->redirect(array('action' => 'index'));
        }
         foreach($arrid as $item){
			if($item != null){
				$files = $this->Guest->find('first',array(
				'conditions'=>array('Guest.id'=>$item),
				'fields'=>'image')
				);
				$file = "files/uploads/guest/".$files['Guest']['image'];
				if(is_file($file)){
					unlink($file);
				}
			}
    		$this->Guest->delete($item);
         }
		$this->Session->setFlash(__('Đã xóa !', true));
		$this->redirect(array('action' => 'index'));
    }
	function admin_delfile($id = null){
		$files = $this->Guest->find('first',array(
			'conditions'=>array('Guest.id'=>$id),
			'fields'=>'image')
			);
		$file = "files/uploads/guest/".$files['Guest']['image'];
		if(is_file($file)){
			unlink($file);
		}
		$this->Guest->query("Update tblguests set image = '' where id='".$id."'");
    }
  
}
?>